/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.chart.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年9月8日 下午2:36:48
 * @Title <B>todo</B>
 * <pre>todo</pre>
 */

public class HorizontalBar extends View {

	/**
	 * <pre>Constructor</pre>
	 * @param context
	 */

	public HorizontalBar(Context context) {
		this(context,null,0);
	}

	/**
	 * <pre>Constructor</pre>
	 * @param context
	 * @param attrs
	 */

	public HorizontalBar(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}

	/**
	 * <pre>Constructor</pre>
	 * @param context
	 * @param attrs
	 * @param defStyleAttr
	 */

	public HorizontalBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}
	
	private String[] yTextLabel=new String[]{"远程分析","检查项","复诊","除诊","随访"};
	private Paint yLablePaint;
	private Paint yLinePaint;
	private Paint xPaint;
	int yStep=0;
	
	private int[]xValues=new int[]{124,56,49,82,18,91};
	private int[]xColors=new int[]{Color.BLACK,Color.RED,Color.BLUE,Color.CYAN,Color.GREEN,Color.LTGRAY};
	
	private void init() {
		// 绘制 Y坐标		
		yLablePaint=new Paint();
		yLablePaint.setTextAlign(Align.RIGHT);
		yLablePaint.setTextSize(sp2px(20));
		yLablePaint.setAntiAlias(true);
		yLablePaint.setStyle(Paint.Style.FILL);
		yLablePaint.setColor(Color.BLACK);
		// 竖线
		yLinePaint=new Paint();
		yLinePaint.setColor(Color.DKGRAY);
		//
		xPaint=new Paint();
		xPaint.setAntiAlias(true);// 抗锯齿效果
		xPaint.setStyle(Paint.Style.FILL);
		xPaint.setTextSize(sp2px(15));// 字体大小
		//
	}
	
	
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		drawYLable(canvas);
		drawXSeries(canvas);
		
	}
	
	private void drawXSeries(Canvas canvas){
		
		int width=getWidth()-2*dp2px(90);
		float xStep=width/100.0f;	
		for (int i=0;i<xValues.length;i++) {
			xPaint.setColor(xColors[i]);// 字体颜色
			
			RectF rect = new RectF();
			rect.left =dp2px(90);//x
			rect.top =yStep*i+dp2px(20);//y
			
			rect.right =rect.left+(xValues[i]/10.0f)*xStep;//y
			rect.bottom = yStep*(i+1)-dp2px(20);
			System.out.println(rect);
			canvas.drawRect(rect, xPaint);
		}
	}
	
	
	private void drawYLable(Canvas canvas) {
		int leftHeight = getHeight() - dp2px(5);// 左侧外周的 需要划分的高度：
		yStep = leftHeight / yTextLabel.length;// 分成四部分
		// 设置左部的数字
		for (int i = 0; i < yTextLabel.length; i++) {
			canvas.drawText(yTextLabel[i], dp2px(80), dp2px(50) + i * yStep, yLablePaint);
		}
		//左侧的竖线
		canvas.drawLine(dp2px(90), 0,dp2px(90),(float)getHeight(),yLinePaint);
	}
	
	private int dp2px(int value) {
		float v = getContext().getResources().getDisplayMetrics().density;
		return (int) (v * value + 0.5f);
	}

	private int sp2px(int value) {
		float v = getContext().getResources().getDisplayMetrics().scaledDensity;
		return (int) (v * value + 0.5f);
	}
	
}
